package com.zgjkhis.modules.system.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zgjkhis.annotation.AnonymousAccess;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.system.domain.Store;
import com.zgjkhis.modules.system.domain.StoreUser;
import com.zgjkhis.modules.system.service.StoreService;
import com.zgjkhis.modules.system.service.StoreUserService;
import com.zgjkhis.modules.system.service.UserService;
import com.zgjkhis.modules.system.service.dto.StoreDto;
import com.zgjkhis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @date 2018-11-23
 */
@Api(tags = "门店管理：门店管理")
@RestController
@RequestMapping("/store")
public class StoreController {

    private static final String ENTITY_NAME = "store";
    private final StoreService storeService;
    private final StoreUserService storeUserService;
    private final UserService userService;

    public StoreController(StoreService storeService, StoreUserService storeUserService, UserService userService) {
        this.storeService = storeService;
        this.storeUserService = storeUserService;
        this.userService = userService;
    }

    @Log("新增门店")
    @ApiOperation("新增门店")
    @PostMapping(value = "/add")
//    @PreAuthorize("@el.check('supplier:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Store resources) {
        if (resources.getId() != null) {
            throw new ServiceException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        resources.setCreateUser(SecurityUtils.getUserId().toString());
        storeService.save(resources);
        StoreUser storeUser = new StoreUser();
        storeUser.setUserId(SecurityUtils.getUserId().toString());
        storeUser.setStoreId(resources.getId().toString());
        storeUserService.save(storeUser);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("修改门店")
    @ApiOperation("修改门店")
    @PostMapping(value = "/update")
//    @PreAuthorize("@el.check('supplier:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Store resources) {
        Store temp = storeService.getById(resources.getId());
        if (!temp.getCreateUser().equals(SecurityUtils.getUserId().toString())) {
            throw new ServiceException("您没有权限修改");
        }
        storeService.saveOrUpdate(resources);
        return ResponseEntity.ok(true);
    }


    @Log("获取门店列表")
    @ApiOperation("获取门店列表")
    @PostMapping(value = "/list")
//    @PreAuthorize("@el.check('supplier:edit')")
    public ResponseEntity<Object> list() {
        //个人创建门店
        List<StoreDto> list = storeService.queryStoreListByUserId(SecurityUtils.getUserId());
        return ResponseEntity.ok(list);
    }


    @Log("获取有效门店列表")
    @ApiOperation("获取有效门店列表")
    @PostMapping(value = "/queryList")
    @AnonymousAccess
//    @PreAuthorize("@el.check('supplier:edit')")
    public ResponseEntity<Object> queryList() {
        //个人创建门店
        List<StoreDto> list = storeService.queryListByUserId(SecurityUtils.getUserId());
        return ResponseEntity.ok(list);
    }

    @Log("获取有效门店列表")
    @ApiOperation("获取有效门店列表")
    @PostMapping(value = "/allList")
    @AnonymousAccess
    public ResponseEntity<Object> allList() {
        //个人创建门店
        List<Store> list = storeService.list(Wrappers.<Store>query().lambda().eq(Store::getStatus, 0));
        return ResponseEntity.ok(list);
    }

}
